package com.rjwh.dingdong.client.fragment.menu;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.community.fragment.CommunityPostFragment;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.bean.localbean.ModularTab;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.fragment.BaseFragment;
import com.rjwh.dingdong.client.mvp.presenter.impl.CommunityPresenter;
import com.rjwh.dingdong.client.mvp.view.CommunityView;
import com.rjwh.dingdong.client.view.HackyViewPager;
import com.rjwh.dingdong.client.widget.PagerSlidingTabStrip;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 社区
 *
 * @author 王小珂
 * @ClassName CommunityFragment
 * @Description TODO(这里用一句话描述这个类的作用)
 * @date 2016-3-28 下午4:12:51
 * @history 1.YYYY-MM-DD author: description:
 */
public class CommunityFragment extends BaseFragment implements CommunityView {
    @BindView(R.id.loading_layout)
    RelativeLayout loadingLayout;
    @BindView(R.id.btn_refresh)
    Button btnRefresh;
    @BindView(R.id.error_layout)
    RelativeLayout errorLayout;
    //    @BindView(R.id.fragment_community_chat_tip)
//    ImageView msgRedTip;
    @BindView(R.id.fragment_community_tab_group)
    PagerSlidingTabStrip mPagerSlidingTabStrip;
    @BindView(R.id.fragment_community_pager)
    HackyViewPager mViewPager;

    private static final int MSG_REQUESTCODE = 123;
    public int currentPosition = 0;
    private List<ModularTab> modularlist;
    private CommunityPresenter communityPresenter;
    private List<BaseFragment> fragmentList = new ArrayList<>();

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.community_home_layout, container, false);
        this.communityPresenter = new CommunityPresenter(getActivity(), this);
        ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }

    private void initView() {
        mViewPager.setOffscreenPageLimit(0);// 取消预加载,只加载一个Fragment
    }

    /**
     * mPagerSlidingTabStrip默认值配置
     */
    private void initTabsValue() {
        // 底部游标颜色@color/blue_bg
        mPagerSlidingTabStrip.setIndicatorColor(getResources().getColor(R.color.blue_button_nor));
        mPagerSlidingTabStrip.setTabPaddingLeftRight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics()));
        // tab的分割线颜色
        mPagerSlidingTabStrip.setDividerColor(Color.TRANSPARENT);
        // tab背景
        mPagerSlidingTabStrip.setBackgroundColor(getResources().getColor(R.color.white));
        // tab底线高度
        mPagerSlidingTabStrip.setUnderlineHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics()));
        // 游标高度
        mPagerSlidingTabStrip.setIndicatorHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics()));
        // 选中的文字颜色
        mPagerSlidingTabStrip.setSelectedTextColor(getResources().getColor(R.color.blue_button_nor));
        // 正常文字颜色
        mPagerSlidingTabStrip.setTextColor(Color.BLACK);
        mPagerSlidingTabStrip.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
        mPagerSlidingTabStrip.setShouldExpand(true);
    }

    @Override
    public void showLoadingView() {
        if (loadingLayout != null) {
            loadingLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void hideLoadingView() {
        if (loadingLayout != null) {
            loadingLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void showErrorView() {
        if (errorLayout != null) {
            errorLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void hideErrorView() {
        if (errorLayout != null) {
            errorLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void setModularData(List<ModularTab> modularlist, String keyword) {
        this.modularlist = modularlist;
        for (int i = 0; i < modularlist.size(); i++) {
            fragmentList.add(CommunityPostFragment.getInstance(modularlist.get(i)));
        }
        updateTip();
        MyPagerAdapter adapter = new MyPagerAdapter(getChildFragmentManager());
        mViewPager.setAdapter(adapter);
        mPagerSlidingTabStrip.setViewPager(mViewPager);
        initTabsValue();
    }

    @Override
    public void updateTip() {
//        String unread = MyApplication.spUtil.getStrPreferenceByParamName(MyApplication.app, LocalConstant.SP_COMMUNITY_MSG_UNREAD);
//        if (!AsStrUtils.isEmpty(unread)) {
//            if (unread.equals("1")) {
//                msgRedTip.setVisibility(View.VISIBLE);
//            } else {
//                msgRedTip.setVisibility(View.GONE);
//            }
//        }
    }

    @OnClick({R.id.btn_refresh})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_refresh:
                doGetModularList();
                break;
//            case R.id.fragment_community_chat_btn:
//                // 进入消息列表界面
//                Intent msgIntent = new Intent(getActivity(), CommunityPostMsgActivity.class);
//                startActivityForResult(msgIntent, MSG_REQUESTCODE);
//                break;
//            case R.id.fragment_community_sendposts_btn:
//                // 进入发帖界面
//                if (modularlist != null && !modularlist.isEmpty()) {
//                    Intent publishIntent = new Intent(getActivity(), CommunityPostPublishActivity.class);
//                    publishIntent.putExtra("modularid", modularlist.get(currentPosition).getId());
//                    publishIntent.putExtra("modularname", modularlist.get(currentPosition).getName());
//                    publishIntent.putExtra("modularlist", (Serializable) modularlist);
//                    publishIntent.putExtra("modularfilters", (Serializable) modularlist.get(currentPosition).getFilters());
//                    startActivity(publishIntent);
//                }
//                break;
        }
    }

    /* ***************FragmentPagerAdapter***************** */
    public class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return modularlist.get(position).getName().trim();
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // 切换Fragment后数据不自动刷新
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        doGetModularList();
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == MSG_REQUESTCODE && resultCode == 1) {
            MyApplication.spUtil.setStrPreference(MyApplication.app, LocalConstant.SP_COMMUNITY_MSG_UNREAD, "0");
            updateTip();
        }
    }

    private void doGetModularList() {
        communityPresenter.doGetModularList();
    }

    @Override
    public void onDestroy() {
        communityPresenter.detachView();
        super.onDestroy();
    }
}
